//
//  Recomment.m
//  diaoyuren
//
//  Created by Cheng on 15/12/27.
//  Copyright © 2015年 game. All rights reserved.
//

#import "Recomment.h"
#import "UIImageView+WebCache.h"
#import "NewRecomModel.h"

@interface Recomment()

@property (nonatomic,strong) UIView *bacView;
@property (nonatomic,strong) NewRecomModel *model;

@end
@implementation Recomment

-(UIView *)bacView{
    if (!_bacView) {
        _bacView = [[UIView alloc]initWithFrame:CGRectMake(10, 15, SCREEN_WIDTH-20, H(self)-30)];
        radius(_bacView, 5);
        _bacView.backgroundColor = [UIColor whiteColor];
        _bacView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _bacView.layer.borderWidth = 0.7;
        [self addSubview:_bacView];
    }

    return _bacView;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];//RGBA(200, 200, 200, 1);//[UIColor lightGrayColor];
        
        _image=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, H(self.bacView),  H(self.bacView)-20)];
        [self.bacView addSubview:_image];
        

    }

    return self;
}

-(void)setTitleAry:(NSArray *)titleAry{
   
    _titleAry = [titleAry copy];
    if (_titleAry.count !=0) {
        NewRecomModel *model = _titleAry[0];
        [self.image sd_setImageWithURL:[NSURL URLWithString:model.litpic]];
    }
    
    if (_titleAry.count==0) {
        self.alpha = 0;
    }else{
        self.alpha = 1;
    
    }
    
    for (int i=0; i<_titleAry.count; i++) {
        
        NewRecomModel *model  = _titleAry[i];

        UIButton *bnt = [UIButton buttonWithType:UIButtonTypeSystem];
        
        bnt.tag=i+10;
        
        [bnt addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat x = Width(_image)+5;
        CGFloat height = H(_image) /3;
        CGFloat y = Y(_image) + height*i;
        CGFloat weith =  W(self.bacView)-Width(_image)+5;
        
        bnt.frame = CGRectMake(x,y,weith,height);
        
        [bnt setTitleColor:RGBA(29, 29, 29, 1) forState:UIControlStateNormal];
        [bnt setTitle:model.title forState:UIControlStateNormal];
        bnt.titleLabel.font = font(SCREEN_WIDTH*0.045);
        bnt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        bnt.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 25);
        
        if (i<2) {
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, H(bnt)-0.3,W(bnt) ,0.5 )];
            view.backgroundColor = [UIColor lightGrayColor];
            [bnt addSubview:view];
        }
        
        
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(W(bnt)-25, (H(bnt)-10)/2,5, 10)];
        
        image.image = [UIImage imageNamed:@"icon-arrow"];
        image.alpha = 0.5;
        
        [bnt addSubview:image];
  
        [self.bacView addSubview:bnt];

    }

}
-(void)Click:(UIButton *)sender{
    
    NewRecomModel *model = _titleAry[sender.tag-10];

    if (_Click) {
        _Click(model.skin,model.AtcID);
    }
}

@end
